<?php

namespace app\controller;


use app\BaseController;
use app\dto\PageDto;
use app\dto\UserDto;
use app\model\AppFun;
use app\model\Menu;
use app\model\ModifyQuery;
use app\model\SysClass;
use app\model\SysDictionary;
use app\model\User;
use app\model\BaseQuery;
use app\model\WenZhang;
use app\Request;
use app\service\RedisService;
use app\utils\Md5;
use app\utils\Utils;
use app\validate\WenzhangValidate;
use http\Message\Body;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;


class AppFunController extends BaseController
{


    function pageList(Request $request)
    {
        $params = $request->all();
        $query = BaseQuery::build(AppFun::class);
        if (Utils::isNotNull($params, 'id')) {
            $query->where('id', $params['id']);
        }
//        if (Utils::isNotNull($params, 'parentId')) {
            $query->where('parent_id', $params['parentId']??0);
//        }
        if (Utils::isNotNull($params, 'name')) {
            $query->where('name', 'like', "{$params['name']}%");
        }

        $query->page($params['pageNum'] ?? 1)->limit($params['pageSize'] ?? 10);

        $resultlist = Utils::toHumpForArray($query->select()->toArray());
        /*加入 haveNext */
        foreach ($resultlist as &$item) {
            $item['haveNext'] = AppFun::where('parent_id', $item['id'])->count() > 0;
        }
        $total = $query->count();
        return json([
            "data" => $resultlist,
            "count" => $total
        ]);
    }

    function getItem(Request $request)
    {
        $id = $request->get("id");
        $parentId = $request->get("parentId");
        $query = BaseQuery::build(AppFun::class);
        if (isset($id)) {
            $query->where('id', $id);
        }
        if (isset($parentId)) {
            $query->where('$parentId', $parentId);
        }
        $resultlist = $query->find();
        return Utils::ok($resultlist);
    }

    function add(Request $request)
    {
        $params = $request->all();
        if (!isset($params)) {
            return Utils::failRes("没有参数");
        }
        unset($params['id']);
        if (Utils::isNullPlus($params, 'name')) {
            return Utils::failRes("没有参数,name");
        }
        if (Utils::isNullPlus($params, 'funUrl')) {
            return Utils::failRes("没有参数,funUrl");
        }
        $id = SysDictionary::save($params);
        return utils::ok($id);
    }

    function update(Request $request)
    {
        $params = $request->all();
        /*校验 */
        if (Utils::isNullPlus($params, 'id')) {
            return Utils::failRes("id 必填");
        }
        /*查询是否存在*/
        $query = BaseQuery::build(AppFun::class);
        $one = $query->where("id", $params['id'])->find();
        if (Utils::isNullPlus($one, 'id')) {
            return Utils::failRes("id:{$params['id']} 没有找到数据");
        }
        $resultint = AppFun::update($params, [
            'id' => $params['id']
        ]);

        return Utils::ok($resultint);
    }

    function del(Request $request)
    {
        $id = $request->post("id");
        if (!isset($id)) {
            return Utils::failRes("id参数不能为空");
        }
        $userone = User::find($id);

        if (Utils::isNull($userone)) {
            return Utils::failRes("id: {$id} 找不到数据");
        }

        $result = BaseQuery::build(AppFun::class)->where("id", $id)
            ->useSoftDelete('delete_time', Utils::dateyyyymmddhhmmss(time()))
            ->delete();
        return Utils::ok($result);
    }


}